﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using GameCamera;

namespace Light
{
  public class LightPoint
  {
    Vector4 ambientColor;
    float ambientIntensity;
    Vector4 diffuseColor;
    float diffuseIntensity;
    Vector3 diffusePos;
    Vector4 specularColor;
    float angle;
    Effect lightingEffect;
    Camera camera;
    EffectParameter ambientColorParam, ambientIntensityParam;
    EffectParameter diffuseColorParam, diffuseIntensityParam, diffuseDirectionParam;
    EffectParameter specularColorParam, viewPointParam;
    bool rotating;

    public Vector4 DiffuseColor
    {
      get { return diffuseColor; }
    }

    public float DiffuseIntensity
    {
      get { return diffuseIntensity; }
    }

    public Vector3 DiffusePos
    {
      get { return diffusePos; }
    }

    public Vector4 SpecularColor
    {
      get { return specularColor; }
    }

    public Effect LightingEffect
    {
      get { return lightingEffect; }
      set { lightingEffect = value; }
    }

    public bool Rotate
    {
      get { return rotating; }
      set { rotating = value; }
    }

    public LightPoint(Camera camera, Effect lightingEffect, Vector4 diffuseColor, float diffuseIntensity,
        Vector3 pos, Vector4 specularColor)
    {
      this.diffuseColor = diffuseColor;
      this.diffuseIntensity = diffuseIntensity;
      this.diffusePos = pos;
      this.specularColor = specularColor;
      this.ambientColor = Color.DarkBlue.ToVector4();//new Color(0.2f, 0.2f, 0.2f, 255.0f).ToVector4();
      this.ambientIntensity = 0.25f;
      this.lightingEffect = lightingEffect;
      angle = 0.0f;
      this.camera = camera;
      Vector3 diffuseLightDirection = this.diffusePos;
      diffuseLightDirection.Normalize();
      ambientColorParam = lightingEffect.Parameters["AmbientColor"];
      ambientIntensityParam = lightingEffect.Parameters["AmbientIntensity"];
      diffuseColorParam = lightingEffect.Parameters["DiffuseColor"];
      diffuseIntensityParam = lightingEffect.Parameters["DiffuseIntensity"];
      diffuseDirectionParam = lightingEffect.Parameters["DiffuseDirection"];
      specularColorParam = lightingEffect.Parameters["SpecularColor"];
      viewPointParam = lightingEffect.Parameters["EyePosition"];
      ambientColorParam.SetValue(this.ambientColor);
      ambientIntensityParam.SetValue(this.ambientIntensity);
      diffuseColorParam.SetValue(this.DiffuseColor);
      diffuseIntensityParam.SetValue(this.DiffuseIntensity);
      diffuseDirectionParam.SetValue(diffuseLightDirection);
      specularColorParam.SetValue(this.SpecularColor);
      viewPointParam.SetValue(camera.Position);
    }

    public void Update()
    {

      Vector3 diffuseLightDirection = this.diffusePos;
      diffuseLightDirection.Normalize();
      viewPointParam.SetValue(camera.Position);
      diffuseDirectionParam.SetValue(diffuseLightDirection);
      if (rotating)
        rotate();
    }


    private void rotate()
    {
      const float ROTATION_SPEED = 0.04f;
      //angle += ROTATION_SPEED;
      diffusePos = Vector3.Transform(diffusePos, Matrix.CreateRotationY(ROTATION_SPEED));
    }
  }
}
